/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.games.epkb.controller;

import it.polimi.games.epkb.controller.exceptions.IllegalOrphanException;
import it.polimi.games.epkb.controller.exceptions.NonexistentEntityException;
import it.polimi.games.epkb.controller.exceptions.PreexistingEntityException;
import it.polimi.games.epkb.entityClasses.Activity;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import it.polimi.games.epkb.entityClasses.Component;
import it.polimi.games.epkb.entityClasses.Service;
import java.util.ArrayList;
import java.util.Collection;
import it.polimi.games.epkb.entityClasses.ActivityData;
import it.polimi.games.epkb.entityClasses.ProcessStructure;
import it.polimi.games.epkb.entityClasses.FileUsage;
import javax.transaction.UserTransaction;

/**
 *
 * @author plebani
 */
public class ActivityJpaController implements Serializable {

    public ActivityJpaController(EntityManagerFactory emf) {
        this.utx = null;
        this.emf = emf;
    }


        
    public ActivityJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Activity activity) throws IllegalOrphanException, PreexistingEntityException, Exception {
        if (activity.getServiceCollection() == null) {
            activity.setServiceCollection(new ArrayList<Service>());
        }
        if (activity.getActivityDataCollection() == null) {
            activity.setActivityDataCollection(new ArrayList<ActivityData>());
        }
        if (activity.getProcessStructureCollection() == null) {
            activity.setProcessStructureCollection(new ArrayList<ProcessStructure>());
        }
        if (activity.getProcessStructureCollection1() == null) {
            activity.setProcessStructureCollection1(new ArrayList<ProcessStructure>());
        }
        if (activity.getProcessStructureCollection2() == null) {
            activity.setProcessStructureCollection2(new ArrayList<ProcessStructure>());
        }
        if (activity.getFileUsageCollection() == null) {
            activity.setFileUsageCollection(new ArrayList<FileUsage>());
        }
        List<String> illegalOrphanMessages = null;
        Component componentOrphanCheck = activity.getComponent();
        if (componentOrphanCheck != null) {
            Activity oldActivityOfComponent = componentOrphanCheck.getActivity();
            if (oldActivityOfComponent != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Component " + componentOrphanCheck + " already has an item of type Activity whose component column cannot be null. Please make another selection for the component field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Component component = activity.getComponent();
            if (component != null) {
                component = em.getReference(component.getClass(), component.getId());
                activity.setComponent(component);
            }
            Collection<Service> attachedServiceCollection = new ArrayList<Service>();
            for (Service serviceCollectionServiceToAttach : activity.getServiceCollection()) {
                serviceCollectionServiceToAttach = em.getReference(serviceCollectionServiceToAttach.getClass(), serviceCollectionServiceToAttach.getId());
                attachedServiceCollection.add(serviceCollectionServiceToAttach);
            }
            activity.setServiceCollection(attachedServiceCollection);
            Collection<ActivityData> attachedActivityDataCollection = new ArrayList<ActivityData>();
            for (ActivityData activityDataCollectionActivityDataToAttach : activity.getActivityDataCollection()) {
                activityDataCollectionActivityDataToAttach = em.getReference(activityDataCollectionActivityDataToAttach.getClass(), activityDataCollectionActivityDataToAttach.getActivityDataPK());
                attachedActivityDataCollection.add(activityDataCollectionActivityDataToAttach);
            }
            activity.setActivityDataCollection(attachedActivityDataCollection);
            Collection<ProcessStructure> attachedProcessStructureCollection = new ArrayList<ProcessStructure>();
            for (ProcessStructure processStructureCollectionProcessStructureToAttach : activity.getProcessStructureCollection()) {
                processStructureCollectionProcessStructureToAttach = em.getReference(processStructureCollectionProcessStructureToAttach.getClass(), processStructureCollectionProcessStructureToAttach.getProcessStructurePK());
                attachedProcessStructureCollection.add(processStructureCollectionProcessStructureToAttach);
            }
            activity.setProcessStructureCollection(attachedProcessStructureCollection);
            Collection<ProcessStructure> attachedProcessStructureCollection1 = new ArrayList<ProcessStructure>();
            for (ProcessStructure processStructureCollection1ProcessStructureToAttach : activity.getProcessStructureCollection1()) {
                processStructureCollection1ProcessStructureToAttach = em.getReference(processStructureCollection1ProcessStructureToAttach.getClass(), processStructureCollection1ProcessStructureToAttach.getProcessStructurePK());
                attachedProcessStructureCollection1.add(processStructureCollection1ProcessStructureToAttach);
            }
            activity.setProcessStructureCollection1(attachedProcessStructureCollection1);
            Collection<ProcessStructure> attachedProcessStructureCollection2 = new ArrayList<ProcessStructure>();
            for (ProcessStructure processStructureCollection2ProcessStructureToAttach : activity.getProcessStructureCollection2()) {
                processStructureCollection2ProcessStructureToAttach = em.getReference(processStructureCollection2ProcessStructureToAttach.getClass(), processStructureCollection2ProcessStructureToAttach.getProcessStructurePK());
                attachedProcessStructureCollection2.add(processStructureCollection2ProcessStructureToAttach);
            }
            activity.setProcessStructureCollection2(attachedProcessStructureCollection2);
            Collection<FileUsage> attachedFileUsageCollection = new ArrayList<FileUsage>();
            for (FileUsage fileUsageCollectionFileUsageToAttach : activity.getFileUsageCollection()) {
                fileUsageCollectionFileUsageToAttach = em.getReference(fileUsageCollectionFileUsageToAttach.getClass(), fileUsageCollectionFileUsageToAttach.getId());
                attachedFileUsageCollection.add(fileUsageCollectionFileUsageToAttach);
            }
            activity.setFileUsageCollection(attachedFileUsageCollection);
            em.persist(activity);
            if (component != null) {
                component.setActivity(activity);
                component = em.merge(component);
            }
            for (Service serviceCollectionService : activity.getServiceCollection()) {
                serviceCollectionService.getActivityCollection().add(activity);
                serviceCollectionService = em.merge(serviceCollectionService);
            }
            for (ActivityData activityDataCollectionActivityData : activity.getActivityDataCollection()) {
                Activity oldActivityOfActivityDataCollectionActivityData = activityDataCollectionActivityData.getActivity();
                activityDataCollectionActivityData.setActivity(activity);
                activityDataCollectionActivityData = em.merge(activityDataCollectionActivityData);
                if (oldActivityOfActivityDataCollectionActivityData != null) {
                    oldActivityOfActivityDataCollectionActivityData.getActivityDataCollection().remove(activityDataCollectionActivityData);
                    oldActivityOfActivityDataCollectionActivityData = em.merge(oldActivityOfActivityDataCollectionActivityData);
                }
            }
            for (ProcessStructure processStructureCollectionProcessStructure : activity.getProcessStructureCollection()) {
                Activity oldActivityOfProcessStructureCollectionProcessStructure = processStructureCollectionProcessStructure.getActivity();
                processStructureCollectionProcessStructure.setActivity(activity);
                processStructureCollectionProcessStructure = em.merge(processStructureCollectionProcessStructure);
                if (oldActivityOfProcessStructureCollectionProcessStructure != null) {
                    oldActivityOfProcessStructureCollectionProcessStructure.getProcessStructureCollection().remove(processStructureCollectionProcessStructure);
                    oldActivityOfProcessStructureCollectionProcessStructure = em.merge(oldActivityOfProcessStructureCollectionProcessStructure);
                }
            }
            for (ProcessStructure processStructureCollection1ProcessStructure : activity.getProcessStructureCollection1()) {
                Activity oldActivity1OfProcessStructureCollection1ProcessStructure = processStructureCollection1ProcessStructure.getActivity1();
                processStructureCollection1ProcessStructure.setActivity1(activity);
                processStructureCollection1ProcessStructure = em.merge(processStructureCollection1ProcessStructure);
                if (oldActivity1OfProcessStructureCollection1ProcessStructure != null) {
                    oldActivity1OfProcessStructureCollection1ProcessStructure.getProcessStructureCollection1().remove(processStructureCollection1ProcessStructure);
                    oldActivity1OfProcessStructureCollection1ProcessStructure = em.merge(oldActivity1OfProcessStructureCollection1ProcessStructure);
                }
            }
            for (ProcessStructure processStructureCollection2ProcessStructure : activity.getProcessStructureCollection2()) {
                Activity oldActivity2OfProcessStructureCollection2ProcessStructure = processStructureCollection2ProcessStructure.getActivity2();
                processStructureCollection2ProcessStructure.setActivity2(activity);
                processStructureCollection2ProcessStructure = em.merge(processStructureCollection2ProcessStructure);
                if (oldActivity2OfProcessStructureCollection2ProcessStructure != null) {
                    oldActivity2OfProcessStructureCollection2ProcessStructure.getProcessStructureCollection2().remove(processStructureCollection2ProcessStructure);
                    oldActivity2OfProcessStructureCollection2ProcessStructure = em.merge(oldActivity2OfProcessStructureCollection2ProcessStructure);
                }
            }
            for (FileUsage fileUsageCollectionFileUsage : activity.getFileUsageCollection()) {
                Activity oldIdActivityOfFileUsageCollectionFileUsage = fileUsageCollectionFileUsage.getIdActivity();
                fileUsageCollectionFileUsage.setIdActivity(activity);
                fileUsageCollectionFileUsage = em.merge(fileUsageCollectionFileUsage);
                if (oldIdActivityOfFileUsageCollectionFileUsage != null) {
                    oldIdActivityOfFileUsageCollectionFileUsage.getFileUsageCollection().remove(fileUsageCollectionFileUsage);
                    oldIdActivityOfFileUsageCollectionFileUsage = em.merge(oldIdActivityOfFileUsageCollectionFileUsage);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findActivity(activity.getId()) != null) {
                throw new PreexistingEntityException("Activity " + activity + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Activity activity) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Activity persistentActivity = em.find(Activity.class, activity.getId());
            Component componentOld = persistentActivity.getComponent();
            Component componentNew = activity.getComponent();
            Collection<Service> serviceCollectionOld = persistentActivity.getServiceCollection();
            Collection<Service> serviceCollectionNew = activity.getServiceCollection();
            Collection<ActivityData> activityDataCollectionOld = persistentActivity.getActivityDataCollection();
            Collection<ActivityData> activityDataCollectionNew = activity.getActivityDataCollection();
            Collection<ProcessStructure> processStructureCollectionOld = persistentActivity.getProcessStructureCollection();
            Collection<ProcessStructure> processStructureCollectionNew = activity.getProcessStructureCollection();
            Collection<ProcessStructure> processStructureCollection1Old = persistentActivity.getProcessStructureCollection1();
            Collection<ProcessStructure> processStructureCollection1New = activity.getProcessStructureCollection1();
            Collection<ProcessStructure> processStructureCollection2Old = persistentActivity.getProcessStructureCollection2();
            Collection<ProcessStructure> processStructureCollection2New = activity.getProcessStructureCollection2();
            Collection<FileUsage> fileUsageCollectionOld = persistentActivity.getFileUsageCollection();
            Collection<FileUsage> fileUsageCollectionNew = activity.getFileUsageCollection();
            List<String> illegalOrphanMessages = null;
            if (componentNew != null && !componentNew.equals(componentOld)) {
                Activity oldActivityOfComponent = componentNew.getActivity();
                if (oldActivityOfComponent != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Component " + componentNew + " already has an item of type Activity whose component column cannot be null. Please make another selection for the component field.");
                }
            }
            for (ActivityData activityDataCollectionOldActivityData : activityDataCollectionOld) {
                if (!activityDataCollectionNew.contains(activityDataCollectionOldActivityData)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ActivityData " + activityDataCollectionOldActivityData + " since its activity field is not nullable.");
                }
            }
            for (ProcessStructure processStructureCollectionOldProcessStructure : processStructureCollectionOld) {
                if (!processStructureCollectionNew.contains(processStructureCollectionOldProcessStructure)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ProcessStructure " + processStructureCollectionOldProcessStructure + " since its activity field is not nullable.");
                }
            }
            for (ProcessStructure processStructureCollection1OldProcessStructure : processStructureCollection1Old) {
                if (!processStructureCollection1New.contains(processStructureCollection1OldProcessStructure)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ProcessStructure " + processStructureCollection1OldProcessStructure + " since its activity1 field is not nullable.");
                }
            }
            for (ProcessStructure processStructureCollection2OldProcessStructure : processStructureCollection2Old) {
                if (!processStructureCollection2New.contains(processStructureCollection2OldProcessStructure)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ProcessStructure " + processStructureCollection2OldProcessStructure + " since its activity2 field is not nullable.");
                }
            }
            for (FileUsage fileUsageCollectionOldFileUsage : fileUsageCollectionOld) {
                if (!fileUsageCollectionNew.contains(fileUsageCollectionOldFileUsage)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain FileUsage " + fileUsageCollectionOldFileUsage + " since its idActivity field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (componentNew != null) {
                componentNew = em.getReference(componentNew.getClass(), componentNew.getId());
                activity.setComponent(componentNew);
            }
            Collection<Service> attachedServiceCollectionNew = new ArrayList<Service>();
            for (Service serviceCollectionNewServiceToAttach : serviceCollectionNew) {
                serviceCollectionNewServiceToAttach = em.getReference(serviceCollectionNewServiceToAttach.getClass(), serviceCollectionNewServiceToAttach.getId());
                attachedServiceCollectionNew.add(serviceCollectionNewServiceToAttach);
            }
            serviceCollectionNew = attachedServiceCollectionNew;
            activity.setServiceCollection(serviceCollectionNew);
            Collection<ActivityData> attachedActivityDataCollectionNew = new ArrayList<ActivityData>();
            for (ActivityData activityDataCollectionNewActivityDataToAttach : activityDataCollectionNew) {
                activityDataCollectionNewActivityDataToAttach = em.getReference(activityDataCollectionNewActivityDataToAttach.getClass(), activityDataCollectionNewActivityDataToAttach.getActivityDataPK());
                attachedActivityDataCollectionNew.add(activityDataCollectionNewActivityDataToAttach);
            }
            activityDataCollectionNew = attachedActivityDataCollectionNew;
            activity.setActivityDataCollection(activityDataCollectionNew);
            Collection<ProcessStructure> attachedProcessStructureCollectionNew = new ArrayList<ProcessStructure>();
            for (ProcessStructure processStructureCollectionNewProcessStructureToAttach : processStructureCollectionNew) {
                processStructureCollectionNewProcessStructureToAttach = em.getReference(processStructureCollectionNewProcessStructureToAttach.getClass(), processStructureCollectionNewProcessStructureToAttach.getProcessStructurePK());
                attachedProcessStructureCollectionNew.add(processStructureCollectionNewProcessStructureToAttach);
            }
            processStructureCollectionNew = attachedProcessStructureCollectionNew;
            activity.setProcessStructureCollection(processStructureCollectionNew);
            Collection<ProcessStructure> attachedProcessStructureCollection1New = new ArrayList<ProcessStructure>();
            for (ProcessStructure processStructureCollection1NewProcessStructureToAttach : processStructureCollection1New) {
                processStructureCollection1NewProcessStructureToAttach = em.getReference(processStructureCollection1NewProcessStructureToAttach.getClass(), processStructureCollection1NewProcessStructureToAttach.getProcessStructurePK());
                attachedProcessStructureCollection1New.add(processStructureCollection1NewProcessStructureToAttach);
            }
            processStructureCollection1New = attachedProcessStructureCollection1New;
            activity.setProcessStructureCollection1(processStructureCollection1New);
            Collection<ProcessStructure> attachedProcessStructureCollection2New = new ArrayList<ProcessStructure>();
            for (ProcessStructure processStructureCollection2NewProcessStructureToAttach : processStructureCollection2New) {
                processStructureCollection2NewProcessStructureToAttach = em.getReference(processStructureCollection2NewProcessStructureToAttach.getClass(), processStructureCollection2NewProcessStructureToAttach.getProcessStructurePK());
                attachedProcessStructureCollection2New.add(processStructureCollection2NewProcessStructureToAttach);
            }
            processStructureCollection2New = attachedProcessStructureCollection2New;
            activity.setProcessStructureCollection2(processStructureCollection2New);
            Collection<FileUsage> attachedFileUsageCollectionNew = new ArrayList<FileUsage>();
            for (FileUsage fileUsageCollectionNewFileUsageToAttach : fileUsageCollectionNew) {
                fileUsageCollectionNewFileUsageToAttach = em.getReference(fileUsageCollectionNewFileUsageToAttach.getClass(), fileUsageCollectionNewFileUsageToAttach.getId());
                attachedFileUsageCollectionNew.add(fileUsageCollectionNewFileUsageToAttach);
            }
            fileUsageCollectionNew = attachedFileUsageCollectionNew;
            activity.setFileUsageCollection(fileUsageCollectionNew);
            activity = em.merge(activity);
            if (componentOld != null && !componentOld.equals(componentNew)) {
                componentOld.setActivity(null);
                componentOld = em.merge(componentOld);
            }
            if (componentNew != null && !componentNew.equals(componentOld)) {
                componentNew.setActivity(activity);
                componentNew = em.merge(componentNew);
            }
            for (Service serviceCollectionOldService : serviceCollectionOld) {
                if (!serviceCollectionNew.contains(serviceCollectionOldService)) {
                    serviceCollectionOldService.getActivityCollection().remove(activity);
                    serviceCollectionOldService = em.merge(serviceCollectionOldService);
                }
            }
            for (Service serviceCollectionNewService : serviceCollectionNew) {
                if (!serviceCollectionOld.contains(serviceCollectionNewService)) {
                    serviceCollectionNewService.getActivityCollection().add(activity);
                    serviceCollectionNewService = em.merge(serviceCollectionNewService);
                }
            }
            for (ActivityData activityDataCollectionNewActivityData : activityDataCollectionNew) {
                if (!activityDataCollectionOld.contains(activityDataCollectionNewActivityData)) {
                    Activity oldActivityOfActivityDataCollectionNewActivityData = activityDataCollectionNewActivityData.getActivity();
                    activityDataCollectionNewActivityData.setActivity(activity);
                    activityDataCollectionNewActivityData = em.merge(activityDataCollectionNewActivityData);
                    if (oldActivityOfActivityDataCollectionNewActivityData != null && !oldActivityOfActivityDataCollectionNewActivityData.equals(activity)) {
                        oldActivityOfActivityDataCollectionNewActivityData.getActivityDataCollection().remove(activityDataCollectionNewActivityData);
                        oldActivityOfActivityDataCollectionNewActivityData = em.merge(oldActivityOfActivityDataCollectionNewActivityData);
                    }
                }
            }
            for (ProcessStructure processStructureCollectionNewProcessStructure : processStructureCollectionNew) {
                if (!processStructureCollectionOld.contains(processStructureCollectionNewProcessStructure)) {
                    Activity oldActivityOfProcessStructureCollectionNewProcessStructure = processStructureCollectionNewProcessStructure.getActivity();
                    processStructureCollectionNewProcessStructure.setActivity(activity);
                    processStructureCollectionNewProcessStructure = em.merge(processStructureCollectionNewProcessStructure);
                    if (oldActivityOfProcessStructureCollectionNewProcessStructure != null && !oldActivityOfProcessStructureCollectionNewProcessStructure.equals(activity)) {
                        oldActivityOfProcessStructureCollectionNewProcessStructure.getProcessStructureCollection().remove(processStructureCollectionNewProcessStructure);
                        oldActivityOfProcessStructureCollectionNewProcessStructure = em.merge(oldActivityOfProcessStructureCollectionNewProcessStructure);
                    }
                }
            }
            for (ProcessStructure processStructureCollection1NewProcessStructure : processStructureCollection1New) {
                if (!processStructureCollection1Old.contains(processStructureCollection1NewProcessStructure)) {
                    Activity oldActivity1OfProcessStructureCollection1NewProcessStructure = processStructureCollection1NewProcessStructure.getActivity1();
                    processStructureCollection1NewProcessStructure.setActivity1(activity);
                    processStructureCollection1NewProcessStructure = em.merge(processStructureCollection1NewProcessStructure);
                    if (oldActivity1OfProcessStructureCollection1NewProcessStructure != null && !oldActivity1OfProcessStructureCollection1NewProcessStructure.equals(activity)) {
                        oldActivity1OfProcessStructureCollection1NewProcessStructure.getProcessStructureCollection1().remove(processStructureCollection1NewProcessStructure);
                        oldActivity1OfProcessStructureCollection1NewProcessStructure = em.merge(oldActivity1OfProcessStructureCollection1NewProcessStructure);
                    }
                }
            }
            for (ProcessStructure processStructureCollection2NewProcessStructure : processStructureCollection2New) {
                if (!processStructureCollection2Old.contains(processStructureCollection2NewProcessStructure)) {
                    Activity oldActivity2OfProcessStructureCollection2NewProcessStructure = processStructureCollection2NewProcessStructure.getActivity2();
                    processStructureCollection2NewProcessStructure.setActivity2(activity);
                    processStructureCollection2NewProcessStructure = em.merge(processStructureCollection2NewProcessStructure);
                    if (oldActivity2OfProcessStructureCollection2NewProcessStructure != null && !oldActivity2OfProcessStructureCollection2NewProcessStructure.equals(activity)) {
                        oldActivity2OfProcessStructureCollection2NewProcessStructure.getProcessStructureCollection2().remove(processStructureCollection2NewProcessStructure);
                        oldActivity2OfProcessStructureCollection2NewProcessStructure = em.merge(oldActivity2OfProcessStructureCollection2NewProcessStructure);
                    }
                }
            }
            for (FileUsage fileUsageCollectionNewFileUsage : fileUsageCollectionNew) {
                if (!fileUsageCollectionOld.contains(fileUsageCollectionNewFileUsage)) {
                    Activity oldIdActivityOfFileUsageCollectionNewFileUsage = fileUsageCollectionNewFileUsage.getIdActivity();
                    fileUsageCollectionNewFileUsage.setIdActivity(activity);
                    fileUsageCollectionNewFileUsage = em.merge(fileUsageCollectionNewFileUsage);
                    if (oldIdActivityOfFileUsageCollectionNewFileUsage != null && !oldIdActivityOfFileUsageCollectionNewFileUsage.equals(activity)) {
                        oldIdActivityOfFileUsageCollectionNewFileUsage.getFileUsageCollection().remove(fileUsageCollectionNewFileUsage);
                        oldIdActivityOfFileUsageCollectionNewFileUsage = em.merge(oldIdActivityOfFileUsageCollectionNewFileUsage);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = activity.getId();
                if (findActivity(id) == null) {
                    throw new NonexistentEntityException("The activity with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Activity activity;
            try {
                activity = em.getReference(Activity.class, id);
                activity.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The activity with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<ActivityData> activityDataCollectionOrphanCheck = activity.getActivityDataCollection();
            for (ActivityData activityDataCollectionOrphanCheckActivityData : activityDataCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Activity (" + activity + ") cannot be destroyed since the ActivityData " + activityDataCollectionOrphanCheckActivityData + " in its activityDataCollection field has a non-nullable activity field.");
            }
            Collection<ProcessStructure> processStructureCollectionOrphanCheck = activity.getProcessStructureCollection();
            for (ProcessStructure processStructureCollectionOrphanCheckProcessStructure : processStructureCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Activity (" + activity + ") cannot be destroyed since the ProcessStructure " + processStructureCollectionOrphanCheckProcessStructure + " in its processStructureCollection field has a non-nullable activity field.");
            }
            Collection<ProcessStructure> processStructureCollection1OrphanCheck = activity.getProcessStructureCollection1();
            for (ProcessStructure processStructureCollection1OrphanCheckProcessStructure : processStructureCollection1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Activity (" + activity + ") cannot be destroyed since the ProcessStructure " + processStructureCollection1OrphanCheckProcessStructure + " in its processStructureCollection1 field has a non-nullable activity1 field.");
            }
            Collection<ProcessStructure> processStructureCollection2OrphanCheck = activity.getProcessStructureCollection2();
            for (ProcessStructure processStructureCollection2OrphanCheckProcessStructure : processStructureCollection2OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Activity (" + activity + ") cannot be destroyed since the ProcessStructure " + processStructureCollection2OrphanCheckProcessStructure + " in its processStructureCollection2 field has a non-nullable activity2 field.");
            }
            Collection<FileUsage> fileUsageCollectionOrphanCheck = activity.getFileUsageCollection();
            for (FileUsage fileUsageCollectionOrphanCheckFileUsage : fileUsageCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Activity (" + activity + ") cannot be destroyed since the FileUsage " + fileUsageCollectionOrphanCheckFileUsage + " in its fileUsageCollection field has a non-nullable idActivity field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Component component = activity.getComponent();
            if (component != null) {
                component.setActivity(null);
                component = em.merge(component);
            }
            Collection<Service> serviceCollection = activity.getServiceCollection();
            for (Service serviceCollectionService : serviceCollection) {
                serviceCollectionService.getActivityCollection().remove(activity);
                serviceCollectionService = em.merge(serviceCollectionService);
            }
            em.remove(activity);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Activity> findActivityEntities() {
        return findActivityEntities(true, -1, -1);
    }

    public List<Activity> findActivityEntities(int maxResults, int firstResult) {
        return findActivityEntities(false, maxResults, firstResult);
    }

    private List<Activity> findActivityEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Activity.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Activity findActivity(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Activity.class, id);
        } finally {
            em.close();
        }
    }

    public int getActivityCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Activity> rt = cq.from(Activity.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
